package com.example.klaraservice.controller;

import com.example.klaraservice.pojo.Commodity;
import com.example.klaraservice.service.CommodityService;
import com.example.klaraservice.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author : Glacialerisa
 * @date : 2023/6/5 10:52
 * @desc :
 */
@Api(tags = "商品")
@RestController
@RequestMapping("/api/commodity")
public class CommodityController {
    @Resource
    private CommodityService commodityService;
    @ApiOperation("模糊查询所有商品")
    @GetMapping("/getCom")
    public R getCom(@RequestParam(defaultValue = "1") int pageNum,
                    @RequestParam(defaultValue = "10")int pageSize,
                    @RequestParam(defaultValue = "") String name){
        Map<String, Object> data = commodityService.selectAllLikeName(pageNum, pageSize, name);
                //根据data的数据进行拼接图片url?serviceimpl 使用循环+前缀？
        if (data!=null){
            return R.ok().data("commodity", data);
        }
        return R.error();
    }
    @ApiOperation("模糊查询用户所有商品")
    @GetMapping("/getUserCom")
    public R getUserCom(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10")int pageSize,
            @RequestParam(defaultValue = "") String name,
            @RequestParam int id){
        Map<String, Object> data = commodityService.selectAllLikeNameAndUid(pageNum, pageSize,name,id);
        if (data!=null){
            return R.ok().data("commodity", data);
        }
        return R.error();
    }
    @ApiOperation("修改商品信息")
    @PutMapping("/updateCommodityInfo")
    public R updateCommodityInfo(@RequestBody Commodity commodity){
        boolean result = commodityService.updateCommodityInfo(commodity);
        if (result){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("批量修改商品信息")
    @PutMapping("/updateCommodities")
    public R updateCommodities(@RequestBody List<Commodity> commodityList){
        boolean result = false;
        for (Commodity c:commodityList
             ) {
            result = commodityService.updateCommodityInfo(c);
            if (!result){
                break;
            }
        }
        if (result){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("新增商品信息")
    @PostMapping("/addCommodity")
    public R addCommodity(@RequestBody Commodity commodity){
        boolean result = commodityService.addCommodity(commodity);
        if (result){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //TODO 增删改、根据类型存储图片
}
